<?php

class invoicies extends cms_controller {

    var $inv;

    public function __construct() {
        parent::__construct();
        $this->load->model('invoicies_model', 'invoicies');
    }

    public function index($limit = FALSE, $order = FALSE) {
        $this->pass = $this->invoicies->filter();
        $this->themes->render('invoicies/index', $this->pass);
    }

    function show($number = false) {
        if ($number) {
            $this->pass['invoice'] = $inv = $this->invoicies->is_found(array('inv_num' => $number), array(), 'row', __CLASS__);
            if ($inv->itemes == 'YES' && $inv->cancel == 'NO') {
                $this->load->model('invoicie_items_model', 'invitem');
                $this->pass['items'] = $items = $this->invitem->get(array('inv_num' => $inv->inv_num));
                $this->load->model('seller_items_model', 'dealsmo');
                $this->pass['sdata'] = $data = $this->dealsmo->get(array('inv_num' => $inv->inv_num));
            }
            $this->themes->render('invoicies/show', $this->pass);
        } else {
            show_404();
        }
    }

    function pay($number = false) {
        if ($number) {
            $this->pass['invoice'] = $inv = $this->invoicies->is_found(array('inv_num' => $number,'user_id' => $this->user_id), array(), 'row', __CLASS__);
            if ($this->input->post()) {
                $type = $this->input->post('type');
                if (in_array($type, array('bank', 'wallet'))) {
                    if ($this->{'pay' . $type}($inv)) {
                        $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_pay_success'));
                    } else {
                        $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => 'message_pay_error'));
                    }
                } else {
                    $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => 'message_type_error'));
                }
            }
            $this->themes->render('invoicies/pay', $this->pass);
        } else {
            show_404();
        }
    }

    function paytypes($total = false, $type = false) {
        if (in_array($type, array('bank', 'wallet'))) {
            $pass['total'] = $total;
            $this->load->view($this->themes->Cms_themes() . '/' . __class__ . '/pay' . $type, $pass);
        }
        return false;
    }

    function minum_than_pay($str) {
        $inv = $this->inv;
        if ($str < $inv->must_pay) {
            $this->form_validation->set_message('minum_than_pay', lang('message_pay_less_must'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

    private function paybank($inv = false) {
        if ($inv) {
            if ($this->input->post()) {
                $this->inv = $inv;
                $this->session->set_userdata('post', $this->input->post());
                $this->form_validation->set_rules('amount', lang('label_wallet_amount'), 'required|trim|is_natural_no_zero|callback_minum_than_pay');
                $this->form_validation->set_rules('bank_name', lang('label_wallet_bank'), 'required|trim');
                $this->form_validation->set_rules('sender_name', lang('label_wallet_sender_name'), 'required|trim');
                $this->form_validation->set_rules('account_number', lang('label_wallet_account_number'), 'required|trim');
                $this->form_validation->set_rules('note', lang('label_wallet_note'), 'trim');
                if ($this->form_validation->run()) {
                    $this->load->model('wallet_bank_model');
                    $sql_data = array(
                        'wallet_id' => $inv->inv_num,
                        'bank_name' => $this->input->post('bank_name'),
                        'sender_name' => $this->input->post('sender_name'),
                        'account_number' => $this->input->post('account_number'),
                        'note' => $this->input->post('note'),
                        'type' => 'inv'
                    );
                    if ($this->wallet_bank_model->save($sql_data)) {
                        $data['pay_id'] = $this->session->userdata('tempid');
                        $data['payment_status'] = 'YES';
                        $data['confirm_status'] = 'NO';
                        $data['already_paid'] = $this->input->post('amount');
                        $data['payment_date'] = date("Y-m-d H:i:s");
                        $data['pay_type'] = 'BANK';
                        if ($this->invoicies->save($data, array('inv_num' => $inv->inv_num), TRUE)) {
                            $this->session->unset_userdata('post');
                            $this->session->unset_userdata('tempid');
                            return TRUE;
                        } else {
                            get_save_error();
                        }
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    private function paywallet($inv = false) {
        if ($inv) {
            $account = account_wallet();
            if ($account >= $inv->must_pay) {
                $this->load->model('wallet_model');
                $wallet['user_id'] = $this->user_id;
                $wallet['user_add'] = $this->user_id;
                $wallet['operation'] = 'minus';
                $wallet['amount'] = $inv->must_pay;
                $wallet['type'] = 'inv';
                if ($this->wallet_model->save($wallet)) {
                    $data['pay_id'] = $this->session->userdata('tempid');
                    $data['payment_status'] = 'YES';
                    $data['confirm_status'] = 'NO';
                    $data['already_paid'] = $inv->must_pay;
                    $data['payment_date'] = date("Y-m-d H:i:s");
                    $data['pay_type'] = 'WALLET';
                    if ($this->invoicies->save($data, array('inv_num' => $inv->inv_num), TRUE)) {
                        $this->session->unset_userdata('tempid');
                        return TRUE;
                    } else {
                        return FALSE;
                    }
                } else {
                    return FALSE;
                }
                return FALSE;
            } else {
                return FALSE;
            }
        } else {
            return false;
        }
    }

    function cancel($id = false) {
        if ($id) {
            $inv = $this->invoicies->is_found(array('inv_num' => $id, 'payment_status' => 'NO'), array(), 'row', __CLASS__);
            $save['cancel'] = 'USER';
            if ($this->invoicies->save($save, array('inv_num' => $id), TRUE)) {
                switch ($inv->for) {
                    case 'create_store':
                        $this->load->model('store_model', 'stores');
                        $this->stores->delete(array('invoice_id' => $id), FALSE);
                        break;
                    case 'special_product':
                        break;
                    case 'buyproduct':
                        $this->load->model('invoicie_items_model', 'invitem');
                        $this->invitem->delete(array('inv_num' => $id), FALSE);
                        $this->load->model('seller_items_model', 'dealsmo');
                        $this->dealsmo->delete(array('inv_num' => $id), FALSE);
                        break;
                    case 'upgrade_store':
                        $this->load->model('upgrade_store_model');
                        $this->upgrade_store_model->delete(array('invoice_id' => $id), FALSE);
                        break;
                    case 'advertise':
                        $this->load->model('advertise_model');
                        $data = $this->advertise_model->get(array('inv_num' => $id));
                        if($data){
                            foreach ($data as $dt)
                                @unlink (ADS_PATH."/$dt->image");
                        }
                        $this->advertise_model->delete(array('inv_num' => $id), FALSE);
                        break;
                }
                $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_cancel_success'));
            } else {
                $this->themes->set_message(array('url' => __CLASS__, 'type' => 'error', 'message' => 'message_cancel_error'));
            }
        }
        show_404();
    }

}

?>
